﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Code.BLL;
using QianBuo.Lottery.Web.Areas.Manage.Models;
using Code.Models;

namespace QianBuo.Lottery.Web.Areas.Manage.Controllers
{

    [Authorize(Roles = "Manager_Lottery_Open")]
    public class ChaseTaskController : BaseController
    {
        public ActionResult Index(ChaseTaskModels.IndexModel model)
        {
            if (model.Serch == null)
                model.Serch = new ChaseManage.ChaseTaskSerchModel();
            if (ModelState.IsValid)
            {
                if (model.Page<1)
                    model.Page = 1;
                if (model.PageSize<1)
                    model.PageSize = CacheManage.OptionSite[Option_Site_Key.Site_PageInfo_PageCount].ToInt32(15); ;

                var CM = new ChaseManage();
                var data = CM.GetChaseTasks(model.Serch, null);
                model.SumCount = data.Count();
                if (model.SumCount > 0)
                {
                    model.Tasks = data.OrderByDescending(p => p.AddTime).Skip(model.PageSize * (model.Page - 1)).Take(model.PageSize)
                        .ToArray().Each(p =>
                        {
                            p.WinModelName = CacheManage.WinModel[p.WinModel].Name;
                            p.RatioName = CacheManage.Ratio[p.Ratio].Name;
                        });
                    model.SumTask = data.GroupBy(p => 1).Select(p => new ChaseManage.ChaseTaskItem
                    {
                        TaskMoney=p.Sum(pp=>pp.TaskMoney),
                        ChaseCount=p.Sum(pp=>pp.ChaseCount),
                        ExecuteCount=p.Sum(PP=>PP.ExecuteCount),
                        CancleCount=p.Sum(pp=>pp.CancleCount)
                    }).First();

                }
            }
            return View(model);
        }

        public ActionResult Detail(Guid id, ChaseTaskModels.DetailModel model)
        {
            if ( model.Page < 1)
                model.Page = 1;
            if ( model.PageSize < 1)
                model.PageSize = CacheManage.OptionSite[Option_Site_Key.Site_PageInfo_PageCount].ToInt32(15); ;

            var CM = new ChaseManage();
            model.Task = CM.GetChaseTasks(null, null).FirstOrDefault(p => p.ID == id);
            if (model.Task == null)
                return RedirectToAction("Index");

            model.Task.WinModelName = CacheManage.WinModel[model.Task.WinModel].Name;
            model.Task.RatioName = CacheManage.Ratio[model.Task.Ratio].Name;
            model.Numbers = CM.ParseChaseTaskNumber(model.Task.ChooseNumber).Each(p=>p.BackMoneyToWinMoney *= 100);
            var data = CM.GetChaseTaskDetail(id, null);
            model.SumCount = data.Count();
            if (model.SumCount > 0)
            {
                model.Details = data.OrderBy(p => p.ID).Skip(model.PageSize * (model.Page - 1)).Take(model.PageSize)
                    .ToArray().Each(p =>
                    {
                        p.IssueName = new Issue { LotteryID = (int)p.LotteryType, Number = p.IssueName }.FormatIssueName;
                    });
            }
            return View(model);
        }

        /// <summary>
        /// 撤消追号任务
        /// </summary>
        /// <param name="ID">追号明细ID</param>
        public ActionResult Stop(Guid ID)
        {
            var red = Request["returnurl"] ?? Url.Action("Index");
            var msg = string.Empty;
            var result = -1;
            try
            {
                result = new ChaseManage().CancelTask(ID, ChaseTaskState.SysCancel);
                if (result > 0)
                {
                    msg = "已成功中止！";
                }
                else
                    msg = "操作失败！请稍后重新操作。";
            }
            catch (AppException e)
            {
                msg = e.Message;
            }
            catch (Exception)
            {
                msg = "操作失败！请稍后重新操作。";
            }

            return Content(string.Format("<script>alert('{0}');location.href='{1}';</script>", msg, red));
        }
        /// <summary>
        /// 撤消追号明细
        /// </summary>
        /// <param name="ID">追号明细ID</param>
        public ActionResult Cancle(long ID )
        {
            var red = Request["returnurl"] ?? Url.Action("Index");
            var msg = string.Empty;
            var result = -1;
            try
            {
                result = new ChaseManage().CancelDetail(ID,ChaseTaskState.SysCancel);
                if (result > 0)
                {
                    msg = "已成功撤消！";
                }
                else
                    msg = "操作失败！请稍后重新操作。";
            }
            catch (AppException e)
            {
                msg = e.Message;
            }
            catch (Exception)
            {
                msg = "操作失败！请稍后重新操作。";
            }

            return Content(string.Format("<script>alert('{0}');location.href='{1}';</script>", msg, red));
        }

        /// <summary>
        /// 执行追号任务
        /// </summary>
        /// <param name="ID">追号ID</param>
        public ActionResult Execute(Guid ID)
        { 
            var red = Request["returnurl"] ?? Url.Action("Index");
            var msg = string.Empty;
            var result = -1;
            try
            {
                var CM=new ChaseManage();
                result =CM.ExecChaseTask(ID);
                if (result > 0)
                    msg = "已成功执行！";
                else if (result == 0)
                    msg = "无该期途径";
                else
                    msg = "操作失败！请稍后重新操作。";
            }
            catch (AppException e)
            {
                msg = e.Message;
            }
            catch (Exception)
            {
                msg = "操作失败！请稍后重新操作。";
            }

            return Content(string.Format("<script>alert('{0}');location.href='{1}';</script>", msg, red));
        
        }
    }
}
